import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common';
import { DictService } from './dict.service';
import { CreateDictDto } from './dto/create-dict.dto';
import { FindAllDictDto } from './dto/find-all-dict.dto';
import { UpdateDictDto } from './dto/update-dict.dto';

@Controller('dict')
export class DictController {
  constructor(private readonly dictService: DictService) {}
  @Get('page')
  async findAll(@Query() query: FindAllDictDto) {
    return this.dictService.findAll(query);
  }

  @Get(':id/form')
  findForm(@Param('id') id: number) {
    return this.dictService.findForm(id);
  }

  @Post('')
  async create(@Body() body: CreateDictDto) {
    await this.dictService.create(body);
    return '创建成功';
  }

  @Put(':id')
  async update(@Param('id') id: number, @Body() body: UpdateDictDto) {
    await this.dictService.update(id, body);
    return '编辑成功';
  }

  @Delete(':ids')
  async removeMany(@Param('ids') ids: string) {
    // 将传入的字符串ids（比如"2,3"）转换为数字数组
    const idArray = ids.split(',').map(Number);
    await this.dictService.removeMany(idArray);
    return '删除成功';
  }

  @Get(':typeCode/options')
  async getOptions(@Param('typeCode') typeCode: string) {
    return this.dictService.getOptions(typeCode);
  }
}
